package com.sky.controller.admin;

import com.alibaba.druid.sql.visitor.functions.If;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Description DishController
 * @Author 王富贵
 * @Date 2024-10-06
 */
@RestController
@Api(tags = "管理端菜品模块")
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping("/admin/dish")
    @ApiOperation("新增菜品")
    public Result<String> sava(@RequestBody DishDTO dto) {
        cleanCache("dish_*");
        return dishService.sava(dto);
    }

    @GetMapping("/admin/dish/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dto) {
        cleanCache("dish_*");
        return dishService.page(dto);
    }

    @DeleteMapping("/admin/dish")
    @ApiOperation("删除菜品")
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        cleanCache("dish_*");
        return dishService.deleteByIds(ids);
    }

    @GetMapping("/admin/dish/{dishId}")
    public Result<DishVO> selectDishWithFlavor(@PathVariable("dishId") Long dishId) {
        cleanCache("dish_*");
        return dishService.selectDishWithFlavor(dishId);
    }

    @PutMapping("/admin/dish")
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        cleanCache("dish_*");
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/admin/dish/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        String key = "dish_" + categoryId;
        cleanCache(key);
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if (list != null && list.size() > 0) {
            return Result.success(list);
        }

        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);

        list = dishService.listWithFlavor(dish);
        redisTemplate.opsForValue().set(key, list);


        return Result.success(list);

    }

    private void cleanCache(String p) {
        Set keys = redisTemplate.keys(p);
        redisTemplate.delete(keys);
    }

    @GetMapping("/user/dish/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list2(Long categoryId) {
        String k = "dish_" + categoryId;
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(k);
        if (list != null && list.size() > 0) {
            return Result.success(list);
        }
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品

        list = dishService.listWithFlavor(dish);
        redisTemplate.opsForValue().set(k,list);
        return Result.success(list);
    }

}
